package com.adms.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.adms.domain.User;
import com.adms.service.UserService;
import com.adms.util.AliMessageUtil;

@RequestMapping("common")
@RestController
public class CommonController {

	@Autowired
	private UserService userService;
	
	/**
	 * 发送手机验证码
	 * @param phone 手机号
	 * @return 布尔值
	 */
	@GetMapping("sendCode")
	public Object sendCode(@RequestParam("phone")String phone) {
		return AliMessageUtil.sendCode(phone);
	}
	
	@GetMapping("wechatSendCode")
	public Object wechatSendCode(@RequestParam("phone")String phone) {
		User existUser = userService.getByPhone(phone);
		if(existUser == null) {
			throw new RuntimeException(phone+"对应账号不存在");
		}else {
		/*	if(StringUtil.isNotEmpty(existUser.getOpenid())) {
				throw new RuntimeException(phone+"对应账号已被绑定");
			}*/
		}
		return AliMessageUtil.sendCode(phone);
	}
	
	/**
	 * 检查手机验证码
	 * @param phone 手机号
	 * @param code 验证码
	 * @return 布尔值
	 */
	@GetMapping("checkCode")
	public Object checkCode(@RequestParam("phone")String phone,@RequestParam("code")String code) {
		String cacheCode = AliMessageUtil.CODE_CACHE.get(phone);
		return cacheCode == null?false:cacheCode.equals(code);
	}
}
